package org.opengauss.evaluate.visitor;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.util.ArrayList;
import java.util.List;

/**
 * @author biao.yang
 * @Description:
 * @date 2025/3/24 19:24
 */
public class ClassAnnotationVisitor extends ClassVisitor {

    public ClassAnnotationVisitor() {
        super(Opcodes.ASM9);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        return new MethodVisitor(Opcodes.ASM9) {
            // 存储当前方法的SQL内容
            private String sql;

            @Override
            public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
                // 检查是否为MyBatis的SQL注解（如@Select）
                if (isTargetSqlAnnotation(descriptor)) {
                    return new AnnotationVisitor(Opcodes.ASM9) {
                        @Override
                        public void visit(String name, Object value) {
                            // 处理单字符串value（如@Select("SELECT *")）
                            if ("value".equals(name) && value instanceof String) {
                                sql = (String) value;
                            }
                        }

                        @Override
                        public AnnotationVisitor visitArray(String name) {
                            // 处理字符串数组value（如@Select({"SELECT *", "FROM table"})）
                            if ("value".equals(name)) {
                                return new AnnotationVisitor(Opcodes.ASM9) {
                                    List<String> sqlParts = new ArrayList<>();

                                    @Override
                                    public void visit(String name, Object value) {
                                        if (value instanceof String) {
                                            sqlParts.add((String) value);
                                        }
                                    }

                                    @Override
                                    public void visitEnd() {
                                        sql = String.join(" ", sqlParts); // 合并数组元素
                                    }
                                };
                            }
                            return super.visitArray(name);
                        }

                        @Override
                        public void visitEnd() {
                            // 打印最终提取的SQL
                            if (sql != null) {
                                System.out.println("Found SQL in method " + name + ":\n" + sql);
                            }
                        }
                    };
                }
                return super.visitAnnotation(descriptor, visible);
            }
        };
    }

    private boolean isTargetSqlAnnotation(String descriptor) {
        return descriptor.equals("Lorg/apache/ibatis/annotations/Select;") ||
                descriptor.equals("Lorg/apache/ibatis/annotations/Insert;") ||
                descriptor.equals("Lorg/apache/ibatis/annotations/Update;") ||
                descriptor.equals("Lorg/apache/ibatis/annotations/Delete;");
    }

    public static void main(String[] args) {
        String sql = "<script> select bpv.*, bpvs.stage_no from biz_pay_voucher bpv  left join biz_pay_voucher_stage bpvs on bpvs.pay_voucher_id = bpv.id  where bpv.case_id in   <foreach collection=\\\"caseIds\\\" item=\\\"caseId\\\" open=\\\"(\\\" close=\\\")\\\" separator=\\\",\\\">    #{caseId}  </foreach> </script>";
        sql = sql.replace("<script>", " ");
        sql = sql.replace("</script>", " ");
        System.out.println(sql);

    }
}
